class Solution {
public:
    int minOperations(vector<int>& nums, int x)
    {
        int sum = 0;
        for(auto e: nums)
        {
            sum += e;
        }
        int target = sum - x;
        if(target < 0)
            return -1;
        
        int ret = -1;

        for(int left = 0, right = 0, tmp = 0;right < nums.size();right++)
        {
            tmp += nums[right];//进窗口
            while(tmp > target)//判断 
            {
                tmp -= nums[left++];
            }
            if(tmp - target == 0)//出窗口
            {
                ret = max(ret,right - left + 1);
            }
        }

        if(ret == -1) 
            return ret;
        else 
            return nums.size() - ret;
    }
};
